package org.helgoboss.mucommander

import org.helgoboss.scala_additions.CurrentPlatform
import org.helgoboss.scala_additions.OperatingSystemClass._
import org.helgoboss.scala_additions.Implicits._
import org.helgoboss.osgi_additions._
import org.helgoboss.osgi_additions.metatype._
import org.helgoboss.file_manager.FileManager
import org.osgi.service.cm.ConfigurationAdmin
import java.io.File
import org.helgoboss.win32_facade.Win32Facade

class MucommanderActivator extends OsgiContext with OsgiProvider with OsgiConfigurationWatcher with OsgiMetaTypeProvider {    
    whenBundleActive {        
        provideMetaType(objectClass)
        
        whenConfigurationUpdated(objectClass.id) { conf =>
            val resultConf = conf match {
                case Some(c) => objectClass.defaultConfig ++ c
                case None => objectClass.defaultConfig
            }
            val exeFile = new File(resultConf("exeFile").asInstanceOf[String])
            val configDir = resultConf.get("configDir").map(_.asInstanceOf[String]).map(new File(_))
            new Mucommander(exeFile = exeFile, configDir = configDir).providesService[FileManager]
        }
    }
    
    lazy val objectClass = ObjectClass (
        id = "org.helgoboss.mucommander",
        name = "muCommander",
        requiredAttributes = List (
            ElementaryAttribute[String](id = "exeFile", name = "muCommander executable path", default = Some(defaultExeFile))
        ),
        optionalAttributes = List (
            ElementaryAttribute[String](id = "configDir", name = "muCommander configuration directory")
        )
    )
    
    lazy val defaultExeFile = CurrentPlatform.osClass match {
        case Windows => (Win32Facade.getProgramFilesDir / """mucommander\mucommander.exe""").windowsPathExpression
        case Unix => "mucommander"
    }
}
